package com.dxy.shardingjdbc.algorithm;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.time.LocalDateTime;
import java.util.Collection;

/**
 * @Description 告警表分库按时间精准算法（必须要有的）
 * @Author dangxianyue
 * @Date 2023/2/9 23:04
 */
public class AlarmSubDatabaseTimePreciseAlgorithm implements PreciseShardingAlgorithm<LocalDateTime> {
    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<LocalDateTime> shardingValue) {
        int year = shardingValue.getValue().getYear();
        for (String dbName : availableTargetNames) {
            if (dbName.endsWith(String.valueOf(year))) {
                return dbName;
            }
        }
        return null;
    }
}
